home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / utils / pcxutils / pcx2traw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  5.6 KB  |  243 lines

  1. /**************************************************************************
  2.  PCX2TRAW - by Lee Hamel (Patch), hamell@cx.pdx.edu, *Avalanche* coder
  3.  July 14th, 1993
  4. **************************************************************************/
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <malloc.h>
  9.  
  10. typedef struct {
  11.         char    manufacturer;
  12.         char    version;
  13.         char    encoding;
  14.         char    bits_per_pixel;
  15.         int     xmin,ymin;
  16.         int     xmax,ymax;
  17.         int     hres;
  18.         int     vres;
  19.         char    palette[48];
  20.         char    reserved;
  21.         char    colour_planes;
  22.         int     bytes_per_line;
  23.         int     palette_type;
  24.         char    filler[58];
  25.            } PCXHEAD;
  26.  
  27. #define         PCXHEADSIZE     sizeof(PCXHEAD)
  28.  
  29. PCXHEAD header;
  30. unsigned int width, depth, bytes, i, i2, showflag = 0;
  31. unsigned char palette[768];
  32. FILE *infile, *outfile;
  33. unsigned char *tempptr, *picture;
  34. char pcxfile[20],rawfile[20],palfile[20];
  35.  
  36. void Read_PCX_Line(unsigned int vidoffset)
  37. {
  38.   unsigned char c, run;
  39.   unsigned int n = 0;
  40.  
  41.   _asm
  42.   {
  43.     cld
  44.     mov         di,[vidoffset]
  45.   }
  46.  
  47.   do
  48.   {
  49.     c = fgetc(infile) & 0xff;
  50.     if ((c & 0xc0) == 0xc0)             /* if it's a run of bytes field */
  51.     {
  52.       run = c & 0x3f;                   /* and off the high bits */
  53.       c = fgetc(infile);                /* get the run byte */
  54.       n += run;                         /* run the byte */
  55.       for (i2 = 0; i2 < run; i2++)
  56.     *picture++ = c;
  57.  
  58.       if (showflag == 0)
  59.       {
  60.     _asm
  61.     {
  62.         mov     ax,0a000h
  63.         mov     es,ax
  64.         mov     al,[c]
  65.         xor     ch,ch
  66.         mov     cl,[run]
  67.         rep     stosb
  68.     }
  69.       }
  70.     }
  71.     else
  72.     {
  73.       n++;
  74.       *picture++ = c;
  75.  
  76.       if (showflag == 0)
  77.       {
  78.     _asm
  79.     {
  80.         mov     ax,0a000h
  81.         mov     es,ax
  82.         mov     al,[c]
  83.         stosb
  84.     }
  85.       }
  86.     }
  87.   }
  88.   while (n < bytes);
  89. }
  90.  
  91. void Unpack_PCX_File(void)
  92. {
  93.   for (i = 0; i < 768; i++)
  94.     palette[i] = palette[i] >> 2;
  95.  
  96.   if (showflag == 0)
  97.   {
  98.     _asm
  99.     {
  100.         mov     ax,0013h
  101.         int     10h
  102.         mov     ax,1012h
  103.         xor     bx,bx
  104.         mov     cx,256
  105.         mov     dx,offset palette
  106.         int     10h
  107.     }
  108.   }
  109.  
  110.   /* make a temporary pointer too */
  111.   tempptr = picture = (unsigned char *) malloc((size_t) 64000);
  112.   for (i = 0; i < depth; i++)
  113.       Read_PCX_Line(i * 320);
  114. }
  115.  
  116. void Dump_PCX(void)
  117. {
  118.     outfile = fopen(palfile,"wb");
  119.     for (i = 0; i < 768; i++)
  120.     fprintf(outfile,"%c",palette[i]);
  121.     fclose(outfile);
  122.  
  123.     outfile = fopen(rawfile,"wb");
  124.  
  125.     /* start at pixel 0 */
  126.     picture = tempptr;
  127.     for (i = 0; i < 16000; i++)
  128.     {
  129.     fprintf(outfile,"%c",*picture);
  130.     picture += 4;
  131.     }
  132.  
  133.     /* start at pixel 1 */
  134.     tempptr++;
  135.     picture = tempptr;
  136.     for (i = 0; i < 16000; i++)
  137.     {
  138.     fprintf(outfile,"%c",*picture);
  139.     picture += 4;
  140.     }
  141.  
  142.     /* start at pixel 2 */
  143.     tempptr++;
  144.     picture = tempptr;
  145.     for (i = 0; i < 16000; i++)
  146.     {
  147.     fprintf(outfile,"%c",*picture);
  148.     picture += 4;
  149.     }
  150.  
  151.     /* start at pixel 3 */
  152.     tempptr++;
  153.     picture = tempptr;
  154.     for (i = 0; i < 16000; i++)
  155.     {
  156.     fprintf(outfile,"%c",*picture);
  157.     picture += 4;
  158.     }
  159.  
  160.     fclose(outfile);
  161.  
  162.     if (showflag == 1)
  163.     {
  164.     _asm {
  165.          mov ax,0003h
  166.          int 10h
  167.          }
  168.     }
  169. }
  170.  
  171. void Help(void)
  172. {
  173.   printf("\n");
  174.   printf("PCX2TRAW - Converts a PCX pic to tweaked mode RAW format\n");
  175.   printf("           by Patch (hamell@rigel.cs.pdx.edu)           \n");
  176.   printf("────────────────────────────────────────────────────────\n");
  177.   printf("Usage: pcx2traw PCXFILE [-SHOW]\n");
  178.   printf("where: PCXFILE    - the PCX file to read (no extension)\n");
  179.   printf("       -SHOW      - show the PCX to the screen\n\n");
  180.   printf("Example call: pcx2traw picture\n");
  181.   printf("- This will read the file PICTURE and output the palette to PICTURE.PAL\n");
  182.   printf("  and the image data to PICTURE.RAW.  The raw format is this: every\n");
  183.   printf("  4th byte is saved for plane 0, then 1, then 2, then 3.  So pixels\n");
  184.   printf("  0, 4, 8, etc. will be saved (16000 bytes), then plane 1 pixels start\n");
  185.   printf("  for the next 16000 bytes (pixels 1, 5, 9, etc.) and so on for all 4\n");
  186.   printf("  planes.  Can then be output using REP MOVSD for a count of 4000 for\n");
  187.   printf("  each plane.  Fast as hell.\n");
  188.   exit(1);
  189. }
  190.  
  191. void main(int argc, char *argv[])
  192. {
  193.   if (argc == 1) Help();
  194.  
  195.   {
  196.     strcpy(pcxfile,argv[1]);
  197.     strcpy(rawfile,argv[1]);
  198.     strcpy(palfile,argv[1]);
  199.     strcat(pcxfile,".pcx");
  200.     strcat(rawfile,".raw");
  201.     strcat(palfile,".pal");
  202.  
  203.     if ((infile = fopen(pcxfile,"rb")) != NULL)
  204.     {
  205.       if (fread((char *)&header,1,PCXHEADSIZE,infile) == PCXHEADSIZE)
  206.       {
  207.     if (header.manufacturer == 0x0a && header.version == 5)
  208.     {
  209.       if (!fseek(infile,-769L,SEEK_END))
  210.       {
  211.         if (fgetc(infile) == 0x0c && fread(palette,1,768,infile) == 768)
  212.         {
  213.           fseek(infile,128L,SEEK_SET);
  214.           width = header.xmax - header.xmin + 1;
  215.           depth = header.ymax - header.ymin + 1;
  216.           bytes = header.bytes_per_line;
  217.  
  218.           /*
  219.           printf("Width = %d\n",width);
  220.           printf("Depth = %d\n",depth);
  221.           printf("Bytes = %d\n",bytes);
  222.           printf("Chars per row = %d\n",charsrow);
  223.           */
  224.  
  225.           showflag = stricmp(argv[2],"-SHOW");
  226.           Unpack_PCX_File();
  227.           Dump_PCX();
  228.  
  229.           free((unsigned char *) picture);
  230.         }
  231.         else printf("Error reading palette\n");
  232.       }
  233.       else printf("Error seeking to palette\n");
  234.     }
  235.     else printf("Not a 256 color PCX file\n");
  236.       }
  237.       else printf("Error reading %s\n",argv[4]);
  238.       fclose(infile);
  239.     }
  240.     else printf("Error opening %s\n",argv[4]);
  241.   }
  242. }
  243.